package com.example.myretrofit2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myretrofit2.R;
import com.example.myretrofit2.User;
import com.example.myretrofit2.presenter.MyPresenter;
import com.example.myretrofit2.presenter.MySubscriber;

public class InsertActivity extends AppCompatActivity {
EditText username;
EditText pass1;
EditText pass2;
Button zhuce;
User user1 = new User();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert);
        username = findViewById(R.id.username);
        pass1 = findViewById(R.id.pass1);
        pass2 = findViewById(R.id.pass2);
        zhuce = findViewById(R.id.zhuce);
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = username.getText().toString();
                String pass = pass1.getText().toString();
                String pass3 = pass2.getText().toString();
                if (TextUtils.isEmpty(username.getText().toString())) {
                    Toast.makeText(InsertActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                }
                else {
                    if (TextUtils.isEmpty(pass) || TextUtils.isEmpty(pass3)) {
                        Toast.makeText(InsertActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    } else if (!(pass.equals(pass3))) {
                        Toast.makeText(InsertActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    }
                    else if (pass.equals(pass3)) {
                        user1.setLoginId(user);
                        user1.setLoginPass(pass);
                        MyPresenter.QueryUser(user1, new MySubscriber<Boolean>(InsertActivity.this) {
                            @Override
                            public void onNext(Boolean aBoolean) {
                                if(aBoolean)
                                Toast.makeText(InsertActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                                else{
                                    MyPresenter.InsertUser(user1, new MySubscriber<Boolean>(InsertActivity.this) {
                                    @Override
                                    public void onNext(Boolean aBoolean) {
                                        if (aBoolean) {
                                            Intent intent = new Intent(InsertActivity.this, LoginActivity.class);
                                            startActivity(intent);
                                        }
                                    }
                                });

                                }
                            }
                        });
                    }
                }
            }

        });
    }
}